#!/opt/php/bin/php -d display_errors=1
<?php
require_once 'TVB/Message/BaseConsumer.php';
require_once 'TVB/pms/pdes/common/bootstrap.php';
require_once 'TVB/pms/pdes/common/util/P_ProofreadRead.php';
require_once 'TVB/pms/pdes/common/util/MisApi.class.php';
require_once 'TVB/pms/pdes/common/util/MisApiConfig.php';

require_once 'TVB/pms/pdes/common/util/db3/P_Processor.php';
require_once 'TVB/pms/pdes/common/util/db3/PMSDB3Constants.php';
require_once 'TVB/pms/pdes/common/util/db3/P_GetDataFunctions.php';
require_once 'TVB/pms/pdes/common/util/db3/db3dbhandle.php';
require_once 'TVB/pms/pdes/common/util/db3/InfoData.php';

class ConsumerProofread extends BaseConsumer
{
    protected function process(Stomp_Frame $message)
    {
		$start_time = microtime(TRUE);
		echo date('Y-m-d H:i:s') . ": Start import data to #2 Database ...\n";

		$return = true;

		$data   =   $message->map;
		$dbo    =   CommonUtils::getDbConn(1,'preview')->getDbHandle();

		$obj    =   new P_ProofreadRead();
		if (!empty($data->programme_id)) {
			echo "Programme ID: {$data->programme_id}\n";
		} elseif (!empty($data->episode_id)) {
			echo "Episode ID: {$data->episode_id}\n";
		}
		$result =   $obj->getProofread($dbo, $data);

		if ($result === FALSE) {
			$return = $this->sendErrorQueue($message);
		}

		$end_time = microtime(TRUE);
		$time_elapsed = $end_time - $start_time;
		echo date('Y-m-d H:i:s') . ": Import data completed. Time used: {$time_elapsed}s\n";

		return $return ;
	}
}

//Model::$dbo['portal_check_video_r'] = CommonUtils::getDbConn(0, 'portal_check_video');
Model::$dbo['portal_check_video_r'] = Model::$dbo['portal_check_video_w'] = CommonUtils::getDbConn(1, 'portal_check_video');

$cons = new ConsumerProofread(AC::CONFIG_MQ, 'P_ProofreadSend');
try{
	$cons->run();
}catch(Stomp_Exception $e){
	mail(CommonUtils::getAdminMail(),"MQ Server Error, At ".trim(`hostname`) , print_r($e,1));
	echo "MQ Server Error!";
}

?>
